# PHP substr_compare() Function

PHP String Reference

## Example

Compare two strings:

<?php
echo substr_compare("Hello world","Hello world",0);
?>
Run example »

## Definition and Usage

The substr_compare() function compares two strings from a specified start position.

Tip: This function is binary-safe and optionally case-sensitive.

## Syntax

substr_compare(string1,string2,startpos,length,case)

Parameter | Description
--- | ---
string1 | Required. Specifies the first string to compare
string2 | Required. Specifies the second string to compare
startpos | Required. Specifies where to start comparing in string1. If negative, it starts counting from the end of the string
length | Optional. Specifies how much of string1 to compare

case

Optional. A boolean value that specifies whether or not to perform a case-sensitive compare:

- FALSE - Default. Case-sensitive
- TRUE - Case-insensitive

## Technical Details

Return Value:

This function returns:

- 0 - if the two strings are equal
- <0 - if string1 (from startpos) is less than string2
- >0 - if string1 (from startpos) is greater than string2

If length is equal or greater than length of string1, this function returns FALSE.

PHP Version: | 5+
Changelog: | As of PHP 5.1, it is now possible to use a negative startpos

## More Examples

## Example 1

Compare two strings, when start position in string1 for the comparison is 6th:

<?php
echo substr_compare("Hello world","world",6);
?>
Run example »

## Example 2

Using all parameters:

<?php
echo substr_compare("world","or",1,2);
echo substr_compare("world","ld",-2,2);
echo substr_compare("world","orl",1,2);
echo substr_compare("world","OR",1,2,TRUE);
echo substr_compare("world","or",1,3);
echo substr_compare("world","rl",1,2);
?>
Run example »

## Example 3

Different return values:

<?php
echo substr_compare("Hello world!","Hello world!",0); // the two strings are equal
echo substr_compare("Hello world!","Hello",0); // string1 is greater than string2
echo substr_compare("Hello world!","Hello world! Hello!",0); // str1 is less than str2
?>
Run example »

PHP String Reference

### WEB HOSTING

UK Reseller Hosting

### WEB BUILDING

FREE Website BUILDER Free HTML5 Templates

### W3SCHOOLS EXAMS

HTML, CSS, JavaScript, PHP, jQuery, XML, and ASP Certifications

### SHARE THIS PAGE

### Color Picker

### HTML/CSS

HTML Tutorial HTML5 Tutorial CSS Tutorial CSS3 Tutorial Bootstrap Tutorial

### JavaScript

JavaScript Tutorial jQuery Tutorial jQuery Mobile Tutorial AngularJS Tutorial AJAX Tutorial JSON Tutorial

### Graphics

Canvas Tutorial SVG Tutorial Google Maps Tutorial

### Server Side

SQL Tutorial PHP Tutorial ASP Tutorial ASP.NET Tutorial VBScript Tutorial AppML Tutorial

### Web

Web Building Web Statistics Web Certification

### XML

XML Tutorial DTD Tutorial Schema Tutorial XML DOM Tutorial XPath Tutorial XSLT Tutorial XQuery Tutorial XSL-FO Tutorial RSS Tutorial WSDL Tutorial

### HTML/CSS

HTML Tag Reference HTML Event Reference HTML Color Reference CSS 1,2,3 Reference Bootstrap Reference

### JavaScript

JavaScript Reference HTML DOM Reference jQuery Reference jQuery Mobile Reference Google Maps Reference

### Server Side

PHP Reference SQL Reference ASP Reference VBScript Reference Razor Reference ASP.NET Reference

### XML

XML DOM Reference XSLT Reference XSL-FO Reference XPath Reference XQuery Reference Schema Reference RSS Reference SVG Reference

### HTML Charsets

HTML Character Sets HTML ASCII HTML ANSI HTML Windows-1252 HTML ISO-8859-1 HTML Symbols HTML UTF-8

### HTML/CSS

HTML Examples CSS Examples

### Browser Scripting

JavaScript Examples HTML DOM Examples jQuery Examples jQuery Mobile Examples AngularJS Examples AJAX Examples

### Server Scripting

PHP Examples ASP Examples VBScript Examples Razor Examples .NET Examples

### XML Languages

XML Examples DTD Examples XML DOM Examples XSL Examples XSLT Examples XPath Examples XQuery Examples Schema Examples RSS Examples SVG Examples